package com.zuimei.zuimei.widgets;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zuimei.zuimei.R;
import com.zuimei.zuimei.service.DownLoadService;

/**
 * Created by Jogger on 2017/1/26.
 * 下载时弹窗
 */

public class DownLoadDialog extends DialogFragment implements View.OnClickListener {
    private ImageButton ibtn_googleplay, ibtn_wandoujia, ibtn_download;
    private Activity mActivity;
    private String mGoogleplay;
    private String mWandoujia;
    private String mDirect;
    private String mTitle;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.dialog_download, container);
        mActivity=getActivity();
        ibtn_googleplay = (ImageButton) view.findViewById(R.id.ibtn_googleplay);
        ibtn_wandoujia = (ImageButton) view.findViewById(R.id.ibtn_googleplay);
        ibtn_download = (ImageButton) view.findViewById(R.id.ibtn_download);
        ibtn_googleplay.setOnClickListener(this);
        ibtn_wandoujia.setOnClickListener(this);
        ibtn_download.setOnClickListener(this);
        TextView tv_msg = (TextView) view.findViewById(R.id.tv_msg);
        Bundle bundle = getArguments();
        mGoogleplay = bundle.getString("googleplay");
        mTitle = bundle.getString("title");
        if (!"0.0M".equals(bundle.getString("size"))) {
            tv_msg.setText("应用大小：" + bundle.getString("size"));
        }
        if (!TextUtils.isEmpty(mGoogleplay)) {
            ibtn_googleplay.setVisibility(View.VISIBLE);
        }
        mWandoujia = bundle.getString("wandoujia");
        if (!TextUtils.isEmpty(mWandoujia)) {
            ibtn_wandoujia.setVisibility(View.VISIBLE);
        }
        mDirect = bundle.getString("direct");
        if (!TextUtils.isEmpty(mDirect)) {
            ibtn_download.setVisibility(View.VISIBLE);
        }
        return view;
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(mActivity, DownLoadService.class);
        intent.setAction("start.action");
        intent.putExtra("title",mTitle);
        switch (view.getId()) {
            case R.id.ibtn_googleplay:
                intent.putExtra("url", mGoogleplay);
                break;
            case R.id.ibtn_wandoujia:
                intent.putExtra("url", mWandoujia);
                break;
            case R.id.ibtn_download:
                intent.putExtra("url", mDirect);
                break;
        }
        mActivity.startService(intent);
        this.dismiss();
    }
}
